package cn.jingyuan.swan.gw.service.impl;

import cn.jingyuan.bee.exception.SystemException;
import cn.jingyuan.bee.fastjson.FastJsonUtils;
import cn.jingyuan.swan.cloud.core.Result;
import cn.jingyuan.swan.cloud.core.ResultUtils;
import cn.jingyuan.swan.gw.service.AuthorityService;
import cn.jingyuan.swan.gw.service.feign.AuthorityFeignService;
import cn.jingyuan.swan.system.model.dto.AuthorityResourceDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Resource
    AuthorityFeignService authorityFeignService;

    @Override
    public List<AuthorityResourceDto> loadAuthorityResource() {
        Result<List<AuthorityResourceDto>> result = authorityFeignService.findAuthorityResource();
        if (ResultUtils.isError(result)) {
            log.error("加载资源权限错误", FastJsonUtils.json(result));

            // 加载失败抛异常
            throw new SystemException(result.getCode(), result.getCodeMsg(), result.getMessage());
        }

        return result.getData();
    }

}
